package com.example.yt_app.Activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;

import com.example.yt_app.Activity.BaseActivity.SwipeBackBaseActivity;
import com.example.yt_app.AppManager;
import com.example.yt_app.Fragment.EquipmentFragment.BathroomSbFragment;
import com.example.yt_app.R;
import com.example.yt_app.Urils.Delay;
import com.example.yt_app.Urils.Json;
import com.example.yt_app.Urils.SPTool;
import com.example.yt_app.Urils.State;
import com.example.yt_app.Urils.Utils;
import com.example.yt_app.bean.Onenet.JsonRootBean;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.jaredrummler.materialspinner.MaterialSpinner;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 *
 * @author dell
 * @date 2021/7/26
 */
@SuppressWarnings("ALL")
public class AddDevice extends SwipeBackBaseActivity {

    private OkHttpClient okHttpClient;
    @BindView(R.id.detail_toolbar)
    Toolbar toolbar;
    @BindView(R.id.set_bt)
    Button set_bt;
    @BindView(R.id.src_1)
    ImageView Src1;
    @BindView(R.id.sb_id)
    EditText Sb_id;
    @BindView(R.id.spinner)
    MaterialSpinner spinner;
    private String sb_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_adddevice;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(toolbar).init();
        //隐藏状态栏
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init();
        ImmersionBar.with(this).titleBar(R.id.detail_toolbar).keyboardEnable(true).init();
    }
    @Override
    protected void initView() {
        AppManager.getInstance().addActivity(this);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        okHttpClient = new OkHttpClient();

    }

    @Override
    protected void initData() {
        State.OTHER = false;
    }


    @Override
    protected SwipeBackLayout getSwipeBackLayout(int layoutId) {
        return null;
    }




    @Override
    protected void setListener() {
        //toolbar返回按钮监听
        toolbar.setNavigationOnClickListener(v ->finish() );
    }
    @SuppressWarnings("AlibabaSwitchStatement")
    @OnClick(R.id.set_bt)
    public void onClick(View v) {
        //noinspection AlibabaSwitchStatement
        switch (v.getId()){

            case R.id.set_bt:
                DateYanZheng();

                break;
        }
    }
    /**
     * 数据验证
     * */
    @SuppressWarnings({"AlibabaLowerCamelCaseVariableNaming", "AlibabaUndefineMagicConstant"})
    private void DateYanZheng() {
        sb_id = Sb_id.getText().toString();
        Utils.counter = 0;
        //截取之后的字符
        //noinspection AlibabaUndefineMagicConstant
        if (sb_id.length() >= 9 && sb_id != "") {
            switch (State.SCENE){
                case "Bathroom":
                    if (Utils.countStr(State.DeviceIDBathroom,sb_id) >0 ){
                        showMessage("请勿重复添加设备");
                    }else {
                        if (State.DeviceIDBathroom == "") {
                            State.DeviceIDBathroom = sb_id;
                        }else {
                            State.DeviceIDBathroom = State.DeviceIDBathroom+","+sb_id;
                        }
                        Src1.setImageResource(R.mipmap.right);
                        SPTool.getInstance(this).putString("DeviceIDBathroom",State.DeviceIDBathroom);
                        showMessage("添加成功");
                        Delay.Delay_ms(200);
                    }
                    System.out.println(State.DeviceIDBathroom);
                    finish();
                    break;
                case "All":
                    if (Utils.countStr(State.DeviceIDs,sb_id) >0 ){
                        showMessage("请勿重复添加设备");
                    }else {
                        if (State.DeviceIDs == "") {
                            State.DeviceIDs = sb_id;
                        }else {
                            State.DeviceIDs = State.DeviceIDs+","+sb_id;
                        }
                        Src1.setImageResource(R.mipmap.right);
                        SPTool.getInstance(this).putString("DeviceIDs",State.DeviceIDs);
                        showMessage("添加成功");
                        Delay.Delay_ms(200);
                    }
                    System.out.println(State.DeviceIDs);
                    finish();
                    break;
                case "Home":
                    if (Utils.countStr(State.DeviceIDHome,sb_id) >0 ){
                        showMessage("请勿重复添加设备");
                    }else {
                        if (State.DeviceIDHome == "") {
                            State.DeviceIDHome = sb_id;
                        }else {
                            State.DeviceIDHome = State.DeviceIDHome+","+sb_id;
                        }
                        Src1.setImageResource(R.mipmap.right);
                        SPTool.getInstance(this).putString("DeviceIDHome",State.DeviceIDHome);
                        showMessage("添加成功");
                        Delay.Delay_ms(200);
                    }
                    System.out.println(State.DeviceIDHome);
                    finish();
                    break;
                case "Kitchen":
                    if (Utils.countStr(State.DeviceIDKitchen,sb_id) >0 ){
                        showMessage("请勿重复添加设备");
                    }else {
                        if (State.DeviceIDKitchen == "") {
                            State.DeviceIDKitchen = sb_id;
                        }else {
                            State.DeviceIDKitchen = State.DeviceIDKitchen+","+sb_id;
                        }
                        Src1.setImageResource(R.mipmap.right);
                        SPTool.getInstance(this).putString("DeviceIDKitchen",State.DeviceIDKitchen);
                        showMessage("添加成功");
                        Delay.Delay_ms(200);
                    }
                    System.out.println(State.DeviceIDKitchen);
                    finish();
                    break;
                default:break;
            }
        } else {
            Src1.setImageResource(R.mipmap.cuo);
            showMessage("ID格式有误");
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        State.OTHER = true;
    }

}
